package com.gxa.controller.statisticalControl;

import com.gxa.pojo.vo.AdmDataVo;
import com.gxa.pojo.vo.AreaVo;
import com.gxa.pojo.vo.CareLevelVo;
import com.gxa.pojo.vo.OutDataVo;
import com.gxa.service.statistical.AdmStatisService;
import com.gxa.service.statistical.OutRegistService;
import com.gxa.utils.Result;
import com.gxa.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <h3>new-zhyl</h3>
 * <p></p>
 *
 * @author : ch
 * @date : 2024-02-26 10:21
 **/
@RequestMapping("/OutRegist")
@ResponseBody
@RestController
@Api(tags = "OutRegist")
public class OutRegistController {

    @Autowired
    public OutRegistService outRegistService;
    @ApiOperation("今日外出人数统计")
    @RequestMapping(value = "/TodaysFile", method = RequestMethod.GET)
    public Result<String> selecttTdaysTotal() {

        return ResultUtils.buildSuccess(outRegistService.selecttTdaysTotal());
    }
    @ApiOperation("昨日和今日外出人数对比")
    @RequestMapping(value = "/Difference", method = RequestMethod.GET)
    public Result<String> selectDifference() {

//        System.out.println( userProfileService.selectDifference());

        return ResultUtils.buildSuccess(outRegistService.selectDifference());
    }

    @ApiOperation("查询今年外出总人数")
    @RequestMapping(value = "/thisYearsOutCount", method = RequestMethod.GET)
    public Result<String> selectOutCount() {
        return ResultUtils.buildSuccess(outRegistService.selectCount());
    }
    @ApiOperation("今年外出人数与上一年对比")
    @RequestMapping(value = "/yearDifference", method = RequestMethod.GET)
    public Result<String> selectYearDifference() {
        return ResultUtils.buildSuccess(outRegistService.selectYearDifference());
    }
    @ApiOperation("本月外出人数")
    @RequestMapping(value = "/monthOut", method = RequestMethod.GET)
    public Result<String> selectMonthOut() {
        return ResultUtils.buildSuccess(outRegistService.selectMonthCount());
    }
    @ApiOperation("本月外出人数与上月对比")
    @RequestMapping(value = "/monthDifference", method = RequestMethod.GET)
    public Result<String> selectmonthDifference() {
        return ResultUtils.buildSuccess(outRegistService.selectmonthDifference());
    }
    @ApiOperation("本周外出人数")
    @RequestMapping(value = "/weekOut", method = RequestMethod.GET)
    public Result<String> selectweekOut() {
        return ResultUtils.buildSuccess(outRegistService.selectWeekCount());
    }

    @ApiOperation("本周外出人数与上周对比")
    @RequestMapping(value = "/weekDifference", method = RequestMethod.GET)
    public Result<Integer> selectweekDifference() {
        return ResultUtils.buildSuccess(outRegistService.selectWeekDifference());
    }

    @ApiOperation("根据时间范围批量查询每日入院人数数据")
    @RequestMapping(value = "/CountByDate", method = RequestMethod.GET)
    public Result<List<OutDataVo>> selectCountByDate(String startDate, @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") String endDate) {

        LocalDate recentMonth = LocalDate.now();    //current time

        LocalDate pastMonths = recentMonth.minusMonths(1); //a month ago
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate parsedBeginDate = customParseDate(startDate,pastMonths,formatter);
        //parse endDate str to LocalDate if valid;while method caught exception:use default value(now)
        LocalDate parsedEndDate = customParseDate(endDate,recentMonth.plusDays(1),formatter);

        List<OutDataVo> outDataVos=outRegistService.selectCountByDate(parsedBeginDate,parsedEndDate);

        return ResultUtils.buildSuccess(outDataVos);
    }

    private LocalDate customParseDate(String dateStr,LocalDate defaultDate, DateTimeFormatter formatter) {
        /*
            format invalid => return parsed  value
            format valid   => return default value
         */
        try {
            return LocalDate.parse(dateStr, formatter);
        }catch (Exception e){
            return defaultDate;
        }
    }

    @ApiOperation("外出区域统计")
    @RequestMapping(value = "/selectByArea", method = RequestMethod.GET)
    public Result<AreaVo>  selectByArea(){

        return ResultUtils.buildSuccess(outRegistService.selectByArea());
    }

}
